// pages/salesmans/costRecord/costRecord.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    water_id: '',
    page: 1,
    page_count: 1,
    record_data: [],
    order_data: [],
    order_page_count: 1,
    order_page: 1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var water_id = options.water_id;
    var that = this;
    that.setData({
      water_id: water_id
    });
    that.card_recharge_record();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (that.data.currentTab == 0) {
      var page = that.data.page + 1;
      if (page > that.data.page_count) {
        that.setData({
          noMore: true
        })
        return false;
      }
      that.setData({
        page: page
      })
      that.card_recharge_record();
    } else {
      var page = that.data.order_page + 1;
      if (page > that.data.order_page_count) {
        that.setData({
          noMore: true
        })
        return false;
      }
      that.setData({
        order_page: page
      })
      that.waterPurchaseHistory();
    }
  },

  // 切换选项卡
  switchtab: function(e) {
    var that = this;
    if (e.target.dataset.current == 0) {
      that.setData({
        order_data: [],
        order_page: 1,
      });
      that.card_recharge_record();
    } else {
      that.setData({
        record_data: [],
        page: 1,
      });
      that.waterPurchaseHistory();
    }
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  card_recharge_record() {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/card_recharge_record',
      method: "POST",
      data: {
        page: that.data.page,
        row: 15,
        water_id: that.data.water_id
      },
      success: function(res) {
        var flag = res.data;
        if (flag.code > 0) {
          var record = that.data.record_data.concat(flag.data.data)
          that.setData({
            record_data: record,
            page_count: flag.data.page.page_count,
          });
        }
      }
    })
  },
  waterPurchaseHistory() {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/waterPurchaseHistory',
      method: "POST",
      data: {
        page: that.data.page,
        row: 10,
        id: that.data.water_id
      },
      success: function(res) {
        var flag = res.data;
        if (flag.code > 0) {
          var record = that.data.order_data.concat(flag.data.data)
          that.setData({
            order_data: record,
            order_page_count: flag.data.page.page_count,
          });
        }
      }
    })
  },
})